package com.controller.task;

import java.io.IOException;
import java.io.Writer;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.DateUtils;
import com.common.easyui.EasyuiUtils; 
import com.dao.sys.SysManageMapper;
import com.dao.sys.SysUserMapper;
import com.dao.task.AtInforMapper;
import com.dao.task.AtProjectMapper;
import com.dao.task.AtTransferMapper;
import com.dao.task.SysMessageMapper;
import com.dao.task.TaskMapper;
import com.model.sys.SysUser;
import com.model.task.AtInfor;
import com.model.task.AtProject;
import com.model.task.AtTransfer;
import com.model.task.SysMessage;
import com.service.sys.WXService;
import com.service.task.TaskSurpviseService;

import freemarker.template.Template;
import freemarker.template.TemplateException;

@Controller
public class SurpviseController {
	static final Logger logger = LoggerFactory.getLogger(SurpviseController.class);
	@Autowired
	private AtInforMapper atInforMapper;
	@Autowired
	private TaskMapper taskMapper;
	@Autowired
	private TaskSurpviseService taskSurpviseService;
	@Autowired
	private AtProjectMapper atProjectMapper;
	@Autowired
	private AtTransferMapper atTransferMapper;
	@Autowired
	private SysManageMapper sysManageMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	SysMessageMapper sysMessageMapper;
	@Autowired
	WXService wxService;

	@RequestMapping("/task/surpvise")
	public String taskSurpvise(HttpServletRequest request) {
		request.setAttribute("currentyear", DateUtils.getCurrentYear());
		request.setAttribute("currentweek", DateUtils.getWeek(new Date()));
		request.setAttribute("currentdate", DateUtils.getCurrentDate());
		return "task/SurpviseManage";
	}

	@RequestMapping("/SurpviseManage/surpviseView")
	public String surpviseView(HttpServletRequest request) {
		String infId = request.getParameter("inf_id");
		String year = request.getParameter("year");
		String week = request.getParameter("week");
		String currentdate = request.getParameter("currentDate");
		if (currentdate == null) {
			currentdate = DateUtils.getCurrentDate();
		}
		request.setAttribute("year", year);
		request.setAttribute("week", week);
		request.setAttribute("currentdate", currentdate);

		AtInfor model = atInforMapper.selectByPrimaryKey(infId);
		request.setAttribute("model", model);

		HashMap<String, Object> hashtable = new HashMap<>();
		hashtable.put("inf_id", infId);
		hashtable.put("pror_week", year + week);
//		hashtable.put("createtime", currentdate);
		List<HashMap<String, Object>> list = taskMapper.selectDetail(hashtable);

		request.setAttribute("contentlist", list);
		request.setAttribute("infId", infId);
		return "task/TaskDetail";
	}

	@RequestMapping("/SurpviseManage/surpviseViewBack")
	public String surpviseViewBack(HttpServletRequest request) {
		String infId = request.getParameter("infId");
		String year = "", date = "";
		String currentdate = request.getParameter("currentdate");
		Date cDate = DateUtils.convertStringToDate(currentdate);
		Date newDate = DateUtils.addDays(cDate, -7);
		String sunDay = DateUtils.getTimeInterval(newDate).split(",")[1];
		newDate = DateUtils.convertStringToDate(sunDay);
		date = DateUtils.convertDateToString(newDate);
		year = DateUtils.getYear(newDate);
		int week = DateUtils.getWeek(DateUtils.convertStringToDate(date));
		request.setAttribute("year", year);
		request.setAttribute("week", week);
		request.setAttribute("currentdate", date);

		AtInfor model = atInforMapper.selectByPrimaryKey(infId);
		request.setAttribute("model", model);

		HashMap<String, Object> hashtable = new HashMap<>();
		hashtable.put("inf_id", infId);
		hashtable.put("pror_week", year + week);
		hashtable.put("createtime", date);
		List<HashMap<String, Object>> list = taskMapper.selectDetail(hashtable);

		request.setAttribute("contentlist", list);
		request.setAttribute("infId", infId);
		return "task/TaskDetail";
	}

	@RequestMapping("/SurpviseManage/surpviseViewNext")
	public String surpviseViewNext(HttpServletRequest request) {
		String infId = request.getParameter("infId");
		String year = "", date = "";
		String currentdate = request.getParameter("currentdate");
		Date cDate = DateUtils.convertStringToDate(currentdate);
		Date newDate = DateUtils.addDays(cDate, 7);
		String sunDay = DateUtils.getTimeInterval(newDate).split(",")[1];
		newDate = DateUtils.convertStringToDate(sunDay);
		date = DateUtils.convertDateToString(newDate);
		year = DateUtils.getYear(newDate);
		int week = DateUtils.getWeek(DateUtils.convertStringToDate(date));
		request.setAttribute("year", year);
		request.setAttribute("week", week);
		request.setAttribute("currentdate", date);

		AtInfor model = atInforMapper.selectByPrimaryKey(infId);
		request.setAttribute("model", model);

		HashMap<String, Object> hashtable = new HashMap<>();
		hashtable.put("inf_id", infId);
		hashtable.put("pror_week", year + week);
		hashtable.put("createtime", date);
		List<HashMap<String, Object>> list = taskMapper.selectDetail(hashtable);

		request.setAttribute("contentlist", list);
		request.setAttribute("infId", infId);
		return "task/TaskDetail";
	}

	@RequestMapping("/SurpviseManage/exportExcel")
	public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.reset();
		request.setCharacterEncoding("utf-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/x-msdownload");

		HashMap<String, Object> hashtable = new HashMap<>();
		String infId = request.getParameter("infId");
		String year = request.getParameter("year");
		String week = request.getParameter("week");
		if (infId != null && !infId.equals("")) {
			hashtable.put("inf_id", infId);
		}
		hashtable.put("pror_week", year + week);
		List<HashMap<String, Object>> listData = taskMapper.selectDetail(hashtable);

		AtInfor model = atInforMapper.selectByPrimaryKey(infId);
		String filename = URLEncoder.encode(model.getInfMettingname() + ".xls", "UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

		String templateName = "task.ftl";
		Template template = com.common.template.FreeMarkerTemplateUtils.getTemplate(templateName);
		Writer writer = response.getWriter();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		try {
			dataMap.put("model", model);
			dataMap.put("year", year);
			dataMap.put("week", week);
			dataMap.put("listData", listData);
			template.process(dataMap, writer);
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		writer.flush();
		writer.close();
	}

	@RequestMapping("/SurpviseManage/exportView")
	public void exportView(HttpServletRequest request, HttpResponse response) {
		String infId = request.getParameter("inf_id");
		String year = request.getParameter("year");
		String week = request.getParameter("week");
		AtInfor model = atInforMapper.selectByPrimaryKey(infId);
		HashMap<String, Object> hashtable = new HashMap<>();
		hashtable.put("inf_id", infId);
		List<HashMap<String, Object>> list = taskMapper.selectDetail(hashtable);

	}

	@RequestMapping(value = "/SurpviseManage/SurpviseListJson", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String SurpviseListJson(HttpServletRequest request) {
		try {
			HashMap<String, Object> hashtable = new HashMap<>();
			String infId = request.getParameter("infId");
			int page = Integer.parseInt(request.getParameter("page"));
			int size = Integer.parseInt(request.getParameter("rows"));
			hashtable.put("PAGENUM", (page - 1) * size);
			hashtable.put("PAGESIZE", size);
			if (infId != null && !infId.equals("")) {
				hashtable.put("infId", infId);
			}
			List<HashMap<String, Object>> list = taskMapper.selectProjects(hashtable);
			String jsonString = JSON.toJSONString(list);
			jsonString = EasyuiUtils.getDatagridPageJson(jsonString, taskMapper.selectProjectsCount(hashtable));
			logger.info("SurpviseListJson:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("SurpviseListJson:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/meetListJson", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String meetListJson(HttpServletRequest request) {
		try {
			HashMap<String, Object> hashMap = new HashMap<>();
			int page = Integer.parseInt(request.getParameter("page"));
			int size = Integer.parseInt(request.getParameter("rows"));
			hashMap.put("PAGENUM", (page - 1) * size);
			hashMap.put("PAGESIZE", size);
			String state = request.getParameter("state");
			if (state != null && state != "") {
				hashMap.put("state", state);
			}
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			hashMap.put("userid", sysUser.getUserid());
			hashMap.put("username", sysUser.getUsername());
			List<HashMap<String, Object>> list = taskMapper.selectInfors(hashMap);
			String jsonString = JSON.toJSONString(list);
			jsonString = EasyuiUtils.getDatagridPageJson(jsonString, taskMapper.selectCount(hashMap));
			logger.info("meetListJson:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("meetListJson:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/selectLastUsers", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String selectLastUsers(HttpServletRequest request) {
		try {
			HashMap<String, Object> hashtable = new HashMap<>();
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			hashtable.put("creatorid", sysUser.getUserid());
			List<HashMap<String, Object>> list = taskMapper.selectLastUsers(hashtable);
			String jsonString = JSON.toJSONString(list);
			logger.info("selectLastUsers:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("selectLastUsers:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/selectTransferUsers", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String selectTransferUsers(HttpServletRequest request) {
		try {
			HashMap<String, Object> hashtable = new HashMap<>();
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			hashtable.put("creatorid", sysUser.getUserid());
			List<HashMap<String, Object>> list = taskMapper.selectTransferUsers(hashtable);
			String jsonString = JSON.toJSONString(list);
			logger.info("selectLastUsers:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("selectLastUsers:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/selectExeUsers", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String selectExeUsers(HttpServletRequest request) {
		try {
			HashMap<String, Object> hashtable = new HashMap<>();
			hashtable.put("inf_id", request.getParameter("inf_id"));
			List<HashMap<String, Object>> list = taskMapper.selectExeUsers(hashtable);
			String jsonString = JSON.toJSONString(list);
			logger.info("selectExeUsers:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("selectExeUsers:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/saveSurpvise", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String saveSurpvise(HttpServletRequest request) {
		try {
			String resultString = "保存成功!";
			String infId = request.getParameter("infId");
			String exeUsers = request.getParameter("exeusers");
			String proId = request.getParameter("proId");
			String deptId = request.getParameter("deptId");
			String deptName = request.getParameter("deptName");
			JSONArray jsonArray = JSON.parseArray(exeUsers);
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			for (Object obj : jsonArray) {
				JSONObject jsonObject = (JSONObject) obj;
				String userName = jsonObject.getString("name");
				String userId = jsonObject.getString("id");
				AtProject model = atProjectMapper.selectByPrimaryKey(infId, userId, proId);
				if (model == null) {
					model = new AtProject();
					model.setProId(UUID.randomUUID().toString());
					model.setProCreatetime(DateUtils.getCurrentTime());
					model.setProCreateorid(sysUser.getUserid());
					model.setProCreateorname(sysUser.getUsername());
					model.setInfId(infId);
					model.setProExecuteuserid(userId);
					model.setProExecuteusername(userName);
					model.setProResult(request.getParameter("info"));
					model.setProContent(request.getParameter("name"));
					model.setProPlanfinishdate(request.getParameter("planDate"));
					model.setProExecutedeptid(deptId);
					model.setProExecutedeptname(deptName);
					model.setProOverdue(0);
					resultString = atProjectMapper.insert(model) > 0 ? "保存成功" : "保存失败!";
				} else {
					model.setInfId(infId);
					model.setProExecuteuserid(userId);
					model.setProId(proId);
					model.setProExecuteusername(userName);
					model.setProResult(request.getParameter("info"));
					model.setProContent(request.getParameter("name"));
					model.setProPlanfinishdate(request.getParameter("planDate"));
					model.setProExecutedeptid(deptId);
					model.setProExecutedeptname(deptName);
					resultString = atProjectMapper.updateByPrimaryKey(model) > 0 ? "保存成功" : "保存失败!";
				}
			}
			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/saveTransfer", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String saveTransfer(HttpServletRequest request) {
		try {
			String resultString = "转发成功!";
			String infId = request.getParameter("infId");
			String exeUsers = request.getParameter("exeusers");
			JSONArray jsonArray = JSON.parseArray(exeUsers);
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			for (Object obj : jsonArray) {
				JSONObject jsonObject = (JSONObject) obj;
				String userName = jsonObject.getString("name");
				String userId = jsonObject.getString("id");
				AtTransfer model = new AtTransfer();
				model.setInfId(infId);
				model.setTraReceiveid(userId);
				model.setTraReceivename(userName);
				model.setTraSendid(sysUser.getUserid());
				model.setTraSendname(sysUser.getLoginname());
				model.setTraTime(DateUtils.getCurrentTime());
				resultString = atTransferMapper.insert(model) > 0 ? "转发成功" : "转发失败!";
			}
			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/transferListJson", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String transferListJson(HttpServletRequest request) {
		try {
			HashMap<String, Object> hashMap = new HashMap<>();
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			hashMap.put("TRA_SENDID", sysUser.getUserid());
			String infId = request.getParameter("infId");
			hashMap.put("inf_id", infId);
			List<HashMap<String, Object>> list = taskMapper.selectTransfer(hashMap);
			String jsonString = JSON.toJSONString(list);
			jsonString = EasyuiUtils.getDatagridPageJson(jsonString, taskMapper.selectTransferCount(hashMap));
			logger.info("transferListJson:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("meetListJson:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/commitSurpvise", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String commitSurpvise(HttpServletRequest request) {
		String resultString = "提交成功!";
		try {
			String infId = request.getParameter("infId");
			String exeUsers = request.getParameter("exeusers");
			String proId = request.getParameter("proId");
			JSONArray jsonArray = JSON.parseArray(exeUsers);
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			String deptId = request.getParameter("deptId");
			String deptName = request.getParameter("deptName");
			for (Object obj : jsonArray) {
				JSONObject jsonObject = (JSONObject) obj;
				String userName = jsonObject.getString("name");
				String userId = jsonObject.getString("id");
				AtProject model = atProjectMapper.selectByPrimaryKey(infId, userId, proId);
				if (model == null) {
					// 提交要先保存
					model = new AtProject();
					model.setProId(UUID.randomUUID().toString());
					model.setProCreatetime(DateUtils.getCurrentTime());
					model.setProCreateorid(sysUser.getUserid());
					model.setProCreateorname(sysUser.getUsername());
					model.setInfId(infId);
					model.setProExecuteuserid(userId);
					model.setProExecuteusername(userName);
					model.setProResult(request.getParameter("info"));
					model.setProContent(request.getParameter("name"));
					model.setProExetime(DateUtils.getCurrentTime());// 提交时间
					model.setProPlanfinishdate(request.getParameter("planDate"));
					model.setProOverdue(0);
					model.setProExecutedeptid(deptId);
					model.setProExecutedeptname(deptName);
					resultString = atProjectMapper.insert(model) > 0 ? "提交成功" : "提交失败!";
				} else {
					model.setInfId(infId);
					model.setProExecuteuserid(userId);
					model.setProId(proId);
					model.setProExecuteusername(userName);
					model.setProResult(request.getParameter("info"));
					model.setProContent(request.getParameter("name"));
					model.setProExetime(DateUtils.getCurrentTime());
					model.setProPlanfinishdate(request.getParameter("planDate"));
					model.setProExecutedeptid(deptId);
					model.setProExecutedeptname(deptName);
					resultString = atProjectMapper.updateByPrimaryKey(model) > 0 ? "提交成功" : "提交失败!";
				}
				SysUser exeUser = sysUserMapper.selectByPrimaryKey(userId); 
				String content = "%s:   您有1条督办信息需要填报，请及时前往督办系统填写每周工作。";
				content = String.format(content, DateUtils.getCurrentTime());
				wxService.sendTxtMessage(exeUser.getWxid(), content);
				SysMessage modelMes = new SysMessage();
				modelMes.setMessageId(UUID.randomUUID().toString());
				modelMes.setMessageReceiveorid(exeUser.getWxid());
				modelMes.setMessageReceiveortime(DateUtils.getCurrentTime());
				sysMessageMapper.insert(modelMes);
				Thread.sleep(300);
			}
			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/saveMeet", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String saveMeet(HttpServletRequest request) {
		try {
			String resultString = "保存成功!";
			String id = request.getParameter("id");
			AtInfor model = atInforMapper.selectByPrimaryKey(id);
			if (model == null) {
				model = new AtInfor();
			}
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			model.setInfId(id);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
			String inforID = "";
			if (id == null || id.equals("")) {
				model = new AtInfor();
				inforID = UUID.randomUUID().toString();
				model.setInfId(inforID);
				model.setInfCreattime(df.format(new Date()));
				model.setInfCreatorid(sysUser.getUserid());
				model.setInfCreatorname(sysUser.getUsername());
				model.setInfState("0");
			}
			String meetName = request.getParameter("meetingname");
			model.setInfMettingname(meetName);
			if (id == null || id.equals("")) {
				resultString = atInforMapper.insert(model) > 0 ? inforID : "保存失败!";
			} else {
				resultString = atInforMapper.updateByPrimaryKey(model) > 0 ? model.getInfId() : "保存失败!";
			}
			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/hasChild", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String hasChild(HttpServletRequest request) {
		try {
			String resultString = "0";
			String infId = request.getParameter("infId");
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");

			HashMap<String, Object> hashMap = new HashMap<>();
			hashMap.put("infId", infId);
			resultString = Integer.toString(taskMapper.hasChild(hashMap));
			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("hasChild:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/delSurpvise", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String delSurpvise(HttpServletRequest request) {
		try {
			String resultString = "删除成功!";
			String userId = request.getParameter("userId");
			String infId = request.getParameter("infId");
			String proId = request.getParameter("proId");
			resultString = atProjectMapper.deleteByPrimaryKey(infId, userId, proId) > 0 ? "删除成功!" : "删除失败!";

			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/delMeet", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String delMeet(HttpServletRequest request) {
		try {
			String resultString = "删除成功!";
			String infId = request.getParameter("inf_id");
			resultString = taskSurpviseService.delMeet(infId) > 0 ? "删除成功!" : "删除失败!";

			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/selectLastInfor", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String selectLastInfor(HttpServletRequest request) {
		try {
			String resultString = "";
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put("creatorid", sysUser.getUserid());
			resultString = JSON.toJSONString(taskMapper.selectLastInfor(hashMap));
			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/closeSurpvise", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String closeSurpvise(HttpServletRequest request) {
		try {
			String resultString = "操作完成!";
			String infId = request.getParameter("infId");
			String proId = request.getParameter("proId");
			String userId = request.getParameter("userId");
			AtProject model = atProjectMapper.selectByPrimaryKey(infId, userId, proId);
			model.setProOverdue(2);// 0为未完成，1为进度100，2为已完成
			resultString = atProjectMapper.updateByPrimaryKey(model) > 0 ? "操作完成!" : "关闭失败!";

			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/backWeek", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String backWeek(HttpServletRequest request) {
		try {
			String year = "", date = "";
			String currentdate = request.getParameter("currentdate");
			Date cDate = DateUtils.convertStringToDate(currentdate);
			Date newDate = DateUtils.addDays(cDate, -7);
			date = DateUtils.convertDateToString(newDate);
			year = DateUtils.getYear(newDate);
			int week = DateUtils.getWeek(DateUtils.convertStringToDate(date));

			String jsonString = String.format("{\"year\":\"%s\",\"week\":\"%s\",\"date\":\"%s\"}", year, week, date);
			logger.info("backWeek:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("backWeek:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/SurpviseManage/nextWeek", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String nextWeek(HttpServletRequest request) {
		try {
			String year = "", date = "";
			String currentdate = request.getParameter("currentdate");
			Date cDate = DateUtils.convertStringToDate(currentdate);
			Date newDate = DateUtils.addDays(cDate, 7);
			date = DateUtils.convertDateToString(newDate);
			year = DateUtils.getYear(newDate);
			int week = DateUtils.getWeek(DateUtils.convertStringToDate(date));

			String jsonString = String.format("{\"year\":\"%s\",\"week\":\"%s\",\"date\":\"%s\"}", year, week, date);
			logger.info("backWeek:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("backWeek:{}", e.getMessage());
			return e.getMessage();
		}
	}
}
